package com.goods.business.service.imp;

import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductStockMapper productStockMapper;

    @Override
    public Map findProductList(Integer pageNum, Integer pageSize, String name, Long[] categorys, String supplier, int status) {
        //定义一个categoryId，id值为传过来的数组长度-1
        Long categoryId = null;
        if (categorys != null && categorys.length>1){
            categoryId = categorys[categorys.length-1];
        }else {
            categoryId = null;
        }

//        List<Product> productList = productMapper.findProductList(name,categoryId,status);

//        List<ProductVO> productVOList = new ArrayList<>();
//        if (!CollectionUtils.isEmpty(productList)){
//            for (Product product : productList) {
//                ProductVO productVO = new ProductVO();
//                BeanUtils.copyProperties(product,productVO);
//                productVOList.add(productVO);
//            }
//        }
        List<ProductStockVO> productStockVOList = productMapper.selectProductStockVOListByStatus(name,categoryId,status);
        //分页
        List<ProductStockVO> page = ListPageUtils.page(productStockVOList, pageSize, pageNum);
        int total = productStockVOList.size();
        Map map = new HashMap();
        map.put("rows",page);
        map.put("total",total);
        return map;
    }

    @Override
    public void add(ProductVO productVO) {
        Long[] categoryKeys = productVO.getCategoryKeys();
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        if (categoryKeys.length>0) {
            product.setOneCategoryId(categoryKeys[0]);
            if (categoryKeys.length>1) {
                product.setTwoCategoryId(categoryKeys[1]);
                if (categoryKeys.length>2) {
                    product.setThreeCategoryId(categoryKeys[2]);
                }
            }
        }
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        product.setStatus(2);
        product.setPNum(UUID.randomUUID().toString());
        productMapper.insert(product);
    }

    @Override
    public ProductVO edit(Long id) {

        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product,productVO);
        return productVO;
    }

    @Override
    public int updateById(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        Long[] categoryKeys = productVO.getCategoryKeys();
        if (categoryKeys.length>0) {
            product.setOneCategoryId(categoryKeys[0]);
            if (categoryKeys.length>1) {
                product.setTwoCategoryId(categoryKeys[1]);
                if (categoryKeys.length>2) {
                    product.setThreeCategoryId(categoryKeys[2]);
                }
            }
        }
        product.setModifiedTime(new Date());
        int i = productMapper.updateByPrimaryKey(product);
        return i;
    }

    @Override
    public int removeById(Long id) {
        int i = productMapper.removeById(id);
        return i;
    }

    @Override
    public int back(Long id) {
        int i = productMapper.back(id);
        return i;
    }

    @Override
    public int deleteById(Long id) {
        int i = productMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public int publish(Long id) {
        int i = productMapper.back(id);
        return i;
    }

    @Override
    public List<ProductStockVO> findAllStocks(Integer pageNum, Integer pageSize) {
//        List<ProductStockVO> productStockVOList = new ArrayList<>();
//        List<ProductStock> productStockList = new ArrayList<>();
        List<ProductStockVO> productStockVOList = productMapper.selectProductStockVOList();
//        List<Product> productList = productMapper.selectAll();//这个事吧是
//        for (Product product : productList) {
//            String pNum = product.getPNum();
//            ProductStock productStock = productStockMapper.selectByPNum(pNum);
//            productStockList.add(productStock);
//        }
//
//
//        for (ProductStock productStock : productStockList) {
//            if (productStock!=null){
//                ProductStockVO productStockVO = new ProductStockVO();
//                BeanUtils.copyProperties(productStock,productStockVO);
//                productStockVOList.add(productStockVO);
//            }
//
//        }
        //List<ProductStockVO> page = ListPageUtils.page(productStockVOList, pageSize, pageNum);

        return productStockVOList;
    }

   /* @Override
    public Map findProductList(int pageNum, int pageSize, String name, Long categoryId,String supplier, int status) {
        List<Product> productList = productMapper.findProductList(name,categoryId,status);

        List<ProductVO> productVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(productList)){
            for (Product product : productList) {
                ProductVO productVO = new ProductVO();
                BeanUtils.copyProperties(product,productVO);
                productVOList.add(productVO);
            }
        }
        List<ProductVO> page = ListPageUtils.page(productVOList, pageSize, pageNum);
        int total = productVOList.size();
        Map map = new HashMap();
        map.put("rows",page);
        map.put("total",total);
        return map;
    }*/
}
